package com.car.controller.custormer;

import com.car.entity.Custormer;
import com.car.service.custormer.CustomerInfService;
import com.car.util.PageUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 贾勇
 * @ClassName CustomerInfController
 * @Description: 客户信息管理
 * @DateTime: 2021-12-06 15:47
 */
@RestController
public class CustomerInfController {

    @Setter
    @Getter
    @Autowired
    private CustomerInfService customerInfService;

    /**
     * 跳转到客户信息主页面
     *
     * @return
     */
    @RequestMapping("/toClientList")
    public ModelAndView toClientList() {
        return new ModelAndView("client-list");
    }

    /**
     * 得到所有客户信息
     *
     * @param custor
     * @return
     */
    @RequestMapping("/getAllCustomerInf")
    public Map<String, Object> getAllCustomerInf(int pageNum, int pageSize, Custormer custor) {

        Map<String, Object> map = new HashMap<>();

        map.put("custormerName", custor.getCustormerName());
        map.put("custormerPhone", custor.getCustormerPhone());
        map.put("custormer", custor);

        Integer count = customerInfService.getAllCustomerInfCount(map);

        Integer total = PageUtil.getTotalPage(count, pageSize);

        map.put("total", total);

        //通过页码和条数算出本次分页的起始位置
        Integer selectstart = pageSize * (pageNum - 1);

        //通过页码和条数算出本次分页的结束位置
        Integer selectEnd = pageNum * pageSize;

        //将起始位置封装到map中，方便后续dao层调用
        map.put("selectstart", selectstart);

        //将结束位置封装到map中，方便后续dao层调用
        map.put("selectEnd", selectEnd);

        List<Custormer> cuslist = customerInfService.getAllCustomerInf(map);

        map.put("cuslist", cuslist);

        return map;
    }

    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("/clientAdd")
    public ModelAndView add() {
        return new ModelAndView("client-add");
    }

    /**
     * 添加请求
     *
     * @param custormer
     * @return
     */
    @RequestMapping("/addCustomerInf")
    public String addCustomer(Custormer custormer) {

        Integer integer = customerInfService.addCustomerInf(custormer);
        return integer.toString();
    }

    /**
     * 删除请求
     *
     * @param custormerId
     * @return
     */
    @RequestMapping("/delCustomerInf")
    public String delCustomerInf(int custormerId) {
        Integer integer = customerInfService.delCustomerInf(custormerId);
        return integer.toString();
    }

    /**
     * 跳转到修改页面
     *
     * @return
     */
    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(int custormerId) {

        ModelAndView mv = new ModelAndView();
        Custormer cusInfById = customerInfService.getCustomerInfById(custormerId);
        mv.addObject("cusInfById", cusInfById);
        mv.setViewName("client-edit");

        return mv;
    }

    /**
     * 修改请求
     *
     * @param custormer
     * @return
     */
    @RequestMapping("/toUpdateCustomer")
    public String toUpdateCustomer(Custormer custormer) {
        Integer integer = customerInfService.updateCustomerInf(custormer);

        return integer.toString();
    }
}
